package cn.itcast.a_demo;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;

/**
 * 批处理单词统计
 * wordcount.txt
 */
public class BatchWordCount {

    public static void main(String[] args) throws Exception {

        /**
         * 1.获取批处理执行环境
         * 2.加载数据源
         * 3.数据处理转换
         * 4.数据输出打印
         * 5.触发执行
         */
        //1.获取批处理执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        //2.加载数据源
        DataSource<String> source = env.readTextFile("data/wordcount.txt");

        //3.数据处理转换
        source.map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        return Tuple2.of(value, 1);
                    }
                }).groupBy(0)
                .sum(1)
                .print();  //4.数据输出打印 ,在批处理中，打印方法，就是触发算子


        //5.触发执行
        //env.execute();

    }
}
